package com.health.mapp.impl;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;

import com.health.db.model.SubRegisterOrder;
import com.health.db.service.order.ISubRegisterOrderService;
import com.health.mapp.bean.GeneratePreSubOrderReq;
import com.health.mapp.bean.GeneratePreSubOrderResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;
/**
 * 子订单下单
 * @author yuchong
 *
 */
public class GeneratePreSubOrderImpl implements ActionInterface {
	
	private ISubRegisterOrderService subRegisterOrderService;
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
	 
		
		GeneratePreSubOrderReq req = (GeneratePreSubOrderReq) reqBean;
		GeneratePreSubOrderResp _resp = new GeneratePreSubOrderResp();
		
		SubRegisterOrder subOrder = new SubRegisterOrder();
		String type= req.getType();
	
		if (StringUtils.isNotBlank(type)&&StringUtils.isNotBlank(req.getSubOrderType())) {
			//type =0 新增  1 修改   2 删除
			if (type.equals("0")) {
				if (StringUtils.isBlank(req.getOrderId())) {
					_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
					_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
					return _resp;
				}
				
				subOrder =this.copyProperties(req, subOrder);
				//待支付状态
				subOrder.setRunstatusid(Constant.CONSTANTS_ORDER_RUNSTATUS_ID_1);
				subOrder.setRunStatus(Constant.CONSTANTS_ORDER_RUNSTATUS_DES_1);
				subOrder.setCreateUserid(Long.valueOf(req.getUserId()));
				subRegisterOrderService.insert(subOrder);
			}else if(type.equals("1")){
				if (StringUtils.isBlank(req.getOrderId())||StringUtils.isBlank(req.getSubOrderId())) {
					_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
					_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
					return _resp;
				}
				SubRegisterOrder currSubOrder = subRegisterOrderService.selectByPrimaryKey(Long.valueOf(req.getSubOrderId()));
				
				if (null==currSubOrder) {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
					return _resp;
				}else if (currSubOrder.getRunstatusid()!=0) {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3015);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3015_NOTE);
					return _resp;
				}
				
				subOrder =this.copyProperties(req, subOrder);
				subRegisterOrderService.updateOrderSelective(subOrder);
			}else if(type.equals("2")){
				if (StringUtils.isBlank(req.getSubOrderId())) {
					_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
					_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
					return _resp;
				}
				SubRegisterOrder currSubOrder = subRegisterOrderService.selectByPrimaryKey(Long.valueOf(req.getSubOrderId()));
				if (null==currSubOrder) {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
					return _resp;
				}else if (currSubOrder.getRunstatusid()!=5) {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
					return _resp;
				}
			}
			
		}else{
			
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		
		
		
		return _resp;
	}
	public ISubRegisterOrderService getSubRegisterOrderService() {
		return subRegisterOrderService;
	}
	public void setSubRegisterOrderService(			ISubRegisterOrderService subRegisterOrderService) {
		this.subRegisterOrderService = subRegisterOrderService;
	}
	
	public SubRegisterOrder copyProperties(GeneratePreSubOrderReq req,SubRegisterOrder subOrder) throws Exception{
		
		subOrder.setSubOrderId((null==req.getSubOrderId())?null:Long.valueOf(req.getSubOrderId()));
		subOrder.setOrderId((null==req.getOrderId())?null:Long.valueOf(req.getOrderId()));
		subOrder.setRegisterDate(null==req.getRegisterDate()?null:DateUtils.parseDate(req.getRegisterDate()));
		subOrder.setUserId(null==req.getUserId()?null:Long.valueOf(req.getUserId()));
		subOrder.setUserName(req.getUserName());
		subOrder.setAccepterid(null==req.getAccepterid()?null:Long.valueOf(req.getAccepterid()));
		subOrder.setAccepterName(req.getAccepterName());
		subOrder.setServerCharge(null==req.getServerCharge()?null:new BigDecimal(req.getServerCharge()));
		subOrder.setPatientName(req.getPatientName());
		subOrder.setPatientSex(null==req.getPatientSex()?null:Short.valueOf(req.getPatientSex()));	
		subOrder.setPatientBirthday(null==req.getPatientBirthday()?null:DateUtils.parseDate(req.getPatientBirthday()));
		subOrder.setContactName(req.getContactName());
		subOrder.setContactPhone(req.getContactPhone());
		subOrder.setContactAddress(req.getContactAddress());
		subOrder.setEntity(req.getEntity());
		subOrder.setDepartment(req.getDepartment());
		subOrder.setDocName(req.getDocName());
		subOrder.setMediCharge(null==req.getMediCharge()?null:new BigDecimal(req.getMediCharge()));
	    subOrder.setMediName(req.getMediName());
	    subOrder.setSubOrderType(null==req.getSubOrderType()?null:Short.valueOf(req.getSubOrderType()));	
	    subOrder.setUpdateUserid(Long.valueOf(req.getUserId()));
		return subOrder;
	}
	
	

	
	
}
